﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace winform_encryption.Models
{
    internal class MyEncoder
    {
        /// <summary>
        /// 明文
        /// </summary>
        public string ThePlain { get; set; }

        /// <summary>
        /// 密钥
        /// </summary>
        public string TheKey { get; set; }

        /// <summary>
        /// 密文
        /// </summary>
        public string Cypher { get; set; }

        /// <summary>
        /// 加密过程
        /// </summary>
        public void Encode()
        {
            //todo 加密过程
            for (int i = 0, j = 0; i < ThePlain.Length; i++, j++)
            {
                //根据密钥的长度将明文分组与密钥对应
                if (j % TheKey.Length == 0)
                    j = 0;
                int number = (CharToNo(ThePlain[i]) + CharToNo(TheKey[j])) % 26;
                Cypher += Convert.ToChar(number + 'a');
            }
        }

        /// <summary>
        /// 返回指定字符在字母表中的序号，从0开始
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        private int CharToNo(char ch)
        {
            return ch - 'a';
        }
    }
}
